<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\api\controller\Tpmecms;

use think\Db;
use think\Config;
/**
 * 活动🎡
 */
class Activity extends Tpmecms
{
    protected $noNeedLogin = ['get_act_data','act_list','act_list_bug','seckill_act_list'];
    protected $noNeedRight = ['*'];


    public function _initialize()
    {
        parent::_initialize();
    }



    /**
     * 获取活动列表BUG
     *
     * @param integer $page 页1
     * @param integer $limit 数99999
     * @ApiInternal()
     * @return void
     */
    public function act_list_bug($page=1,$limit=99999)
    {
        $store = Db::name('store')->find(24);
        $data['store_id'] = $store['id'];
        $list = Db::name('activity')->where($data)->page($page,$limit)->order('weigh desc')->order('time desc')->select();
        foreach($list as &$val){
            $val['coupons'] = Db::name('coupons')->where('act_id',$val['id'])->find();
        }
        $this->success('ok',$list);
    }


    /**
     * 获取活动列表
     *
     * @param integer $page 页1
     * @param integer $limit 数99999
     * @param integer $sign_status 可不传，1=不需要报名，2=需要报名
     * @ApiSummary ("sign如果有值说明该活动已报名"）
     * @return void
     */
    public function act_list($sign_status = null,$page=1,$limit=10)

    {
        $data['status'] = '1'; 
        if($sign_status){
            $data['sign_status'] = $sign_status; 
        }else{
            $data['sign_status'] = ['<','3']; 
        }
        $list = Db::name('activity')->where($data)->page($page,$limit)->order('weigh desc')->order('time desc')->select();
        foreach($list as &$val){
            if($this->auth->id>0){
                $val['sign'] = Db::name('sign')->where('activity_id',$val['id'])->where('uid',$this->auth->id)->find();
            }else{
                $val['sign'] = null;
            }
            
        }
        $this->success('ok',$list);
    }


    /**
     * 获秒杀取活动列表
     *
     * @param integer $page 页1
     * @param integer $limit 数99999
     * @ApiSummary ("sign如果有值说明该活动已报名"）
     * @return void
     */
    public function seckill_act_list($page=1,$limit=10)
    {
        $data['status'] = '1'; 
        $list = Db::name('activity')->where($data)->page($page,$limit)->where('sign_status','3')->order('weigh desc')->order('time desc')->select();
        foreach($list as &$val){
            if($this->auth->id>0){
                $val['sign'] = Db::name('sign')->where('activity_id',$val['id'])->where('uid',$this->auth->id)->find();
            }else{
                $val['sign'] = null;
            }
            
        }
        $this->success('ok',$list);
    }


    /**
     * 获取活动信息
     *
     * @param int $act_id 活动id
     * @return void
     */
    public function get_act_data($act_id)
    {
        // $act = Db::name('activity')->find($act_id);
        // if(!$act){
        //     $this->error('没有此活动');
        // }
        // $act['coupons'] = Db::name('coupons')->where('act_id',$act['id'])->find();

      
        $this->success('ok',$this->GetActData($act_id));

        $act = $this->GetActData($act_id);
        $this->success('ok',$act);
    }

    /**
     * 添加活动报名
     *
     * @param string $name 姓名
     * @param string $tel 电话
     * @param integer $num 人数
     * @param integer $activity_id 活动id
     * @return void
     */
    public function add_sign($name=null,$tel=null,$num=1,$activity_id=null)
    {
        $act = $this->GetActData($activity_id);
        if($act['status']=='2'){
            $this->error('活动已结束');
        }
        //活动状态:1=无需报名,2=报名,3=秒杀活动
        if($act['sign_status']=='3'){
            if($act['s_time']>time()){
                $this->error('活动开始时间'.date('Y-m-d H:i:s'));
            }
            $data['uid'] = $this->auth->id;
            $data['activity_id'] = $activity_id;
        }else{
            $data['name'] = $name;
            $data['tel'] = $tel;
            $data['num'] = $num;
            $data['uid'] = $this->auth->id;
            $data['activity_id'] = $activity_id;
        }
        
        $res = Db::name('sign')->where($data)->find();


        if($res){
            if($act['sign_status']=='3'){
                $this->error('已抢购过');
            }
            $this->error('已经报名过了');
        }
        $data['name'] = $name;
        $data['tel'] = $tel;
        $data['num'] = $num;
        $data['sign_status'] = $act['sign_status'];
        $res = Db::name('sign')->insert($data);

        $this->success('报名成功');
    }

}